package com.cn.rest.web;

import com.cn.rest.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by chenyuncong on 2019/3/7.
 */
@RestController
@RequestMapping("user")
public class UserController {

    /**
     * 登录
     *
     * @param username 用户名|必填
     * @param password 密码
     * @return 当前登录用户的基本信息
     * @resp code 返回码(0000表示登录成功,其它表示失败)|string|必填
     * @resp msg 登录信息|string
     * @resp username 登录成功后返回的用户名|string
     */
    @ResponseBody
    @PostMapping("login")
    public Map<String, String> login(String username, String password) {
        Map<String, String> model = new HashMap<>();
        model.put("code", "0000");
        model.put("msg", "登录成功");
        model.put("username", username);
        return model;
    }


    /**
     * 用户注册
     *
     * @param user :username 用户名|必填
     * @param user :password 密码
     * @return 注册后生成的用户的基本信息
     * @respbody {"id":"123","password":"123456","username":"admin"}
     * @see User
     */
    @ResponseBody
    @RequestMapping(value = "register", method = {RequestMethod.POST, RequestMethod.PUT})
    User register(User user) {
        user.setId(1L);
        return user;
    }

    /**
     * get没有参数访问
     * @return
     */
    @GetMapping("getUser")
    public User getUser(){
        return new User(1L,"小明","男",18);
    }

    /**
     * get基础参数访问
     * @param id
     * @param name
     * @param sex
     * @param age
     * @return
     */
    @GetMapping("getUserParam")
    public User getUserParam(Long id, String name, String sex, Integer age){
        return new User(id,name,sex,age);
    }

    /**
     * get对象访问
     * @param u
     * @return
     */
    @GetMapping("getUserObj")
    public User getUserObj(User u){
        return new User(u.getId(),u.getName(),u.getSex(),u.getAge());
    }

    /**
     * get body作参数访问
     * @param u
     * @return
     */
    @GetMapping("getUserBody")
    public User getUserBody(@RequestBody User u){
        return new User(u.getId(),u.getName(),u.getSex(),u.getAge());
    }


    /**
     * post 无参数访问
     * @return
     */
    @PostMapping("postUser")
    public User postUser(){
        return new User(2L,"小明2","男",20);
    }

    /**
     * post 基础参数访问
     * @param id
     * @param name
     * @param sex
     * @param age
     * @return
     */
    @PostMapping("postUserParam")
    public User postUserParam(Long id, String name, String sex, Integer age){
        return new User(id,name,sex,age);
    }

    /**
     * post 对象访问
     * @param u
     * @return
     */
    @PostMapping("postUserObj")
    public User postUserObj(User u){
        return new User(u.getId(),u.getName(),u.getSex(),u.getAge());
    }

    /**
     * post body作参数访问
     * @param u
     * @return
     */
    @PostMapping("postUserBody")
    public User postUserBody(@RequestBody User u){
        return new User(u.getId(),u.getName(),u.getSex(),u.getAge());
    }

}
